package com.dodo.proiectCore.base.application;

import android.content.res.Configuration;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.azhon.basic.BuildConfig;
import com.azhon.basic.base.CoreApplication;
import com.wanjian.cockroach.Cockroach;
import com.wanjian.cockroach.ExceptionHandler;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Created by  .
 * @时间 2018/5/14 17:20
 * @描述 ${保存全局变量设计的基本类application}
 */
public class BaseApplication extends CoreApplication {
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化主题
        //AndroidSkin.getInstance().init(this);
    }

    @Override
    public void init() {
        super.init();
        //arouter路由初始化
        //RouterConfig.init(this, BuildConfig.DEBUG);

        //初始化防止APP崩溃 catch
        install();
        //Stetho调试工具初始化
        // Stetho.initializeWithDefaults(this);
        //反射禁止弹窗
        disableAPIDialog();
    }

    private void install() {
        //子线程的异常,建议杀死APP
        final Thread.UncaughtExceptionHandler sysExcepHandler = Thread.getDefaultUncaughtExceptionHandler();
        final Toast toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        //DebugSafeModeUI.init(this);
        Cockroach.install(this, new ExceptionHandler() {
            @Override
            protected void onUncaughtExceptionHappened(Thread thread, Throwable throwable) {
                Log.e("AndroidRuntime", "捕获异常--->onUncaughtExceptionHappened:" + thread + "<---", throwable);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (BuildConfig.DEBUG) {
                            toast.setText("捕获到导致崩溃的异常");
                            toast.show();
                        }
                    }
                });
            }

            @Override
            protected void onBandageExceptionHappened(Throwable throwable) {
                throwable.printStackTrace();//打印警告级别log，该throwable可能是最开始的bug导致的，无需关心
                Log.e("AndroidRuntime", "--->onBandageExceptionHappened:" + "<---");
                if (BuildConfig.DEBUG) {
                    toast.setText("Cockroach Worked");
                    toast.show();
                }
            }

            @Override
            protected void onEnterSafeMode() {
                //好像该方法只会调用一次,首次调用
                if (BuildConfig.DEBUG) {
                Toast.makeText(BaseApplication.this, "已经进入安全模式", Toast.LENGTH_LONG).show();
                //进入界面
                }
            }

            @Override
            protected void onMayBeBlackScreen(Throwable e) {
                Thread thread = Looper.getMainLooper().getThread();
                Log.e("AndroidRuntime", "--->onUncaughtExceptionHappened:" + thread + "<---", e);
                //黑屏时建议直接杀死app
                // sysExcepHandler.uncaughtException(thread, new RuntimeException("black screen"));
            }

        });

    }

    /**
     * 反射 禁止弹窗
     */
    private void disableAPIDialog(){
        if (Build.VERSION.SDK_INT < 28)return;
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
